package com.lnkj.spcj.ui.upload

import android.content.Context
import android.view.WindowManager
import com.lnkj.spcj.R
import com.lnkj.spcj.base.BaseActivity
import com.lnkj.spcj.ui.scan.ScanResultBean
import com.lnkj.spcj.util.ImageLoader
import com.lnkj.spcj.util.oss.OSSOperUtils
import com.lnkj.spcj.util.utilcode.ToastUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.entity.LocalMedia
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_upload.*
import kotlinx.android.synthetic.main.layout_topbar.*
import java.util.concurrent.TimeUnit

class UploadActivity : BaseActivity<UploadContract.Present>(), UploadContract.View {
    override val mPresenter: UploadContract.Present
        get() = UploadPresent().also { it.attachView(this) }
    override val layoutRes: Int
        get() = R.layout.activity_upload
    var bean: ScanResultBean? = null
    var video_url = ""

    override fun initAll() {
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        initDialog()
        tv_title.text = "上传视频 "
        bean = intent.getSerializableExtra("bean") as ScanResultBean
        video_url = intent.getStringExtra("path")
        tv.text = video_url
        ImageLoader.loadImageLocal(mContext, iv, video_url)
    }

    override fun setListener() {
        iv_left.setOnClickListener {
            onBackPressed()
        }
        iv.setOnClickListener {
            PictureSelector.create(this).externalPictureVideo(video_url)
        }
        tv_submit.setOnClickListener {
            if (video_url.isNullOrEmpty()) {
                ToastUtils.showShort("请重新录制视频")
                return@setOnClickListener
            }
            if (bean == null) {
                ToastUtils.showShort("请重新扫描二维码")
                return@setOnClickListener
            }
            var localMedia = LocalMedia()
            localMedia.path = video_url
            mPresenter?.upLoadFiles(1, mutableListOf(localMedia))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        timerRx?.dispose()
    }

    var timerRx: Disposable? = null
    override fun processLogic() {
        timerRx = Observable.timer(500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe {
            tv_submit.performClick()
        }
    }

    override fun getContext(): Context {
        return mContext
    }

    override fun onEmpty() {

    }

    override fun onError() {

    }

    override fun onUploadStart() {
        showDialog()
    }

    override fun onUploadFinish() {
        hideDialog()
    }

    override fun onUploadProgress(s: String) {
        dialog?.setText(s)
    }

    override fun upLoadFiles(type: Int, arrayList: MutableList<String>) {
        when (type) {
            1 -> {
                bean?.data?.also {
                    mPresenter?.saveVideo(OSSOperUtils.AliYunOSSURLFile + arrayList[0], it.station_id
                            ?: "", it.company_id, it.department_id)
                }
            }
        }
    }

    override fun saveVideo() {
        finish()
    }
}